package ca.cbc.medialib.util;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public enum ApplicationContextUtil {
	instance;
	private ClassPathXmlApplicationContext cfx;
	private String contextFileName = "applicationContext.xml";
	
	private void buildApplicationContext() {
		cfx = new ClassPathXmlApplicationContext(contextFileName);
		cfx.registerShutdownHook();
	}
	
	public ClassPathXmlApplicationContext getApplicationContext() {
		if(cfx == null) {
			buildApplicationContext();
		}
		return cfx;
	}
	
	public ClassPathXmlApplicationContext getApplicationContext(String contextFileName) {
		if(!this.contextFileName.equals(contextFileName)) {
			this.contextFileName = contextFileName;
			buildApplicationContext();
		}
		
		return getApplicationContext();
	}
}
